﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StatusBar : MonoBehaviour
{
    private RectTransform rectTransform;

    #region Inspector Attributes
    [SerializeField]
    private RectTransform fill;
    [SerializeField]
    private Text status;
    [SerializeField]
    private int maxValue;
    #endregion
    private int currentValue;

    public int CurrentValue
    {
        get => currentValue;
        set
        {
            currentValue = Mathf.Clamp(value, 0, maxValue);

            var ratio = (float)currentValue / maxValue;

            var offsetX = (ratio - 1.0f) * rectTransform.sizeDelta.x;

            fill.offsetMax = new Vector2(offsetX, fill.offsetMax.y);
            status.text = $"{currentValue}/{maxValue}";
        }
    }

    public void Start()
    {
        rectTransform = GetComponent<RectTransform>();
        CurrentValue = maxValue >> 1;
    }
}
